package tt.dz.appserver.mvc.manage;

import javax.annotation.Resource;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.pub.Comment;
import tt.dz.appserver.service.business.CommentService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;

/**
 * 评论管理
 * @author USER001
 *
 */

@RestController
@RequestMapping("/manage/pubComment")
public class CommentManager {
	
	/**评论管理*/
	@Resource
	CommentService pubCommentService;
	
	/**
	 * 评论删除
	 * @param commentid		评论id
	 **@param userid		用户id
	 **@param associatetype	关联类型	
	 **@param associateid	关联id
	 **@param comment		评论内容
	 **@param commenttime	评论时间
	 **@param commentaddr	位置
	 **@param validflag		有效标志
	 * @return
	 */

	@RequestMapping(value="/PubCommentDel",produces={"application/json;charset=utf-8"})
	public String pubcommentdel(
		 	@RequestParam(value="commentid") String commentid) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubCommentService.pubcommentdel(commentid);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 评论查询
	 **@param commentid		评论id
	 **@param userid		用户id
	 * @param associatetype	关联类型	
	 * @param associateid	关联id
	 **@param comment		评论内容
	 **@param commenttime	评论时间
	 **@param commentaddr	位置
	 **@param validflag		有效标志
	 * @return
	 */

	@RequestMapping(value="/PubCommentSelect",produces={"application/json;charset=utf-8"})
	public String pubcommentselect(
		 	@RequestParam(value="associatetype") Integer associatetype,
		 	@RequestParam(value="associateid") String associateid) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			List<Comment> resultObject = pubCommentService.pubcommentselect(associatetype, associateid);
			
			String code = Response.SUCCESS.getCode();
			String msg = Response.SUCCESS.getMsg();
				
			resultMappingEntity = Result.responseBody(code, msg, resultObject);
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
}
